package cn.wildfire.chat.app.extendsion;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.android.material.appbar.AppBarLayout;
import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import butterknife.ButterKnife;
import cn.wildfire.chat.app.extendsion.utils.StatusBarUtil;

/**
 * Create by HEKE, 2019/8/13 10:36
 * TODO 基类界面,原程序定义的ToolBar类型比较难看，自定义修改比较麻烦。
 */
public abstract class BaseActivity extends Activity {

    /**
     * modify by HEKE , 2019/8/19 14:30
     * TODO 增加界面跳转的层级级别，非一级界面点击返回按键时，使用默认界面跳转
     */
    private int level = 1;

    public abstract void initViews();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        navigationBarSet();
    }

    /**
     * modify by HEKE , 2019/8/19 14:32
     * TODO 设置界面的层级级别，设置为非1数值时，代表返回按键需要执行系统操作，而非进入后台
     */
    public void setLevel(int level) {
        this.level = level;
    }

    public void navigationBarSet() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            int color = getResources().getColor(R.color.colorPrimary);
            getWindow().setNavigationBarColor(Color.TRANSPARENT);
            getWindow().setStatusBarColor(color);
            StatusBarUtil.setStatusTextColor(true, this);
        }
        try {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED //锁屏状态下显示
                    | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD //解锁
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON //保持屏幕长亮
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); //打开屏幕
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        ButterKnife.bind(this);
        initViews();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        ButterKnife.bind(this);
        initViews();
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
        ButterKnife.bind(this);
        initViews();
    }

    // 设置标题栏
    public void setBarTitle(int id) {
        View view = findViewById(R.id.titleTextView);
        if (view != null) {
            ((TextView) view).setText(id);
        }
    }

    // 返回按钮操作
    public void bindBack() {
        View view = findViewById(R.id.backImageView);
        if (view != null) {
            ((ImageView) view).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    BaseActivity.super.onBackPressed();
                }
            });
        }
    }

    // 隐藏返回按钮
    public void hideBack() {
        View view = findViewById(R.id.backImageView);
        if (view != null) {
            view.setVisibility(View.GONE);
        }
    }

    /**
     * 隐藏toolbar条
     */
    public void hideToolbar() {
        AppBarLayout appBarLayout = findViewById(R.id.appbar);
        if (appBarLayout != null) {
            appBarLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (level == 1) {
                moveTaskToBack(true);
            } else {
                onBackPressed();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
